// pages/register/index.js
var network = require("../../utils/network.js");
Page({

  /**
   * 页面的初始数据
   */

  data: {
    img: ''
  },

  /**
   * 产品注册
   * 
  */
  formSubmit: function (e) {
    // var tel = e.detail.value.tel;
    // var phoneReg = /^1[34578]\d{9}$/;
    if (e.detail.value.username == '') {
      wx.showModal({
        content: '请输入经销商',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.tel == '') {
      wx.showModal({
        content: '请输入经销商联系电话',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.qqweChat == '') {
      wx.showModal({
        content: '请输入QQ号/微信号',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.distributor == '') {
      wx.showModal({
        content: '请输入使用医院',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.department == '') {
      wx.showModal({
        content: '请输入使用使用科室',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.department_link == '') {
      wx.showModal({
        content: '请输入科室联系电话',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.productModel == '') {
      wx.showModal({
        content: '请输入产品型号',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.IMEI == '') {
      wx.showModal({
        content: '请输入设备序列号或IMEI',
        showCancel: false
      })
      return false
    }
    if (e.detail.value.date == '') {
      wx.showModal({
        content: '请输入格式为xxxx-xx-xx的购买日期',
        showCancel: false
      })
      return false
    }
    // if (!phoneReg.test(tel)) {
    //   wx.showModal({
    //     content: '手机号码格式错误',
    //     showCancel: false
    //   })
    //   return false
    // }
    
    let that = this;
    let URL = network.server() + 'product/add';
    let Params = {
      address: e.detail.value.distributor,
      member_name: e.detail.value.username,
      mobile: e.detail.value.tel,
      model: e.detail.value.productModel,
      department: e.detail.value.department,
      department_link: e.detail.value.department_link,
      buy_time: e.detail.value.date,
      link_number: e.detail.value.qqweChat,
      code: e.detail.value.IMEI,
      email: '123',
      image: that.data.imagePath
    };
    if (Params.image !== undefined ){
      let Method = 'POST';
      let Message = '';
      network.requestPost(URL, Params, Method, Message, function (res) {
        console.log('产品注册---', res)
        wx.showModal({
          content: '提交成功',
          showCancel: false
        })
      }, function () {
        wx.showToast({
          title: '提交失败',
        })
      })
    }
    else if(Params.image == undefined ){
      wx.showModal({
        content: '请上传合适的图片',
        showCancel: false
      })
    }
  },
  /**
   * 上传保修卡
  */
  chooseImageTap: function () {
    let that = this;
    wx.showActionSheet({
      itemList: ['从相册选择', '拍照'],
      itemColor: "#12bbcd",
      success: function (res) {
        console.log(res.tapIndex)
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          }
          else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: [type],                   // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res);
        var tempFilePaths = res.tempFilePaths;
        let URL = network.server() + 'up/image-for-api?path=product'
        network.upload(URL, that, tempFilePaths)
        that.setData({
          img: tempFilePaths[0]
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var title = options.title;
    wx.setNavigationBarTitle({
      title: title,
    })
  },
})